package biznussoft.metercor.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import biznussoft.metercor.R;
import biznussoft.metercor.entity.AccountModel;

public class AccountMasterAdapter extends BaseAdapter {
	
	private boolean checkArray[]; 	
	private ArrayList<AccountModel> listdata;
	private Context mContext;     

	public AccountMasterAdapter(Context context, ArrayList<AccountModel> list) {
		this.listdata = list;
		this.mContext = context;
		checkArray = new boolean[list.size()];
	}

	public int getCount() {
		return listdata.size();
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}
	
	public class ViewHolder{
		public TextView name;    
		public TextView phno;
		public ImageView image;
		public RelativeLayout rLayout;
	}

	public View getView(int position, View v, ViewGroup parent) {
		
		ViewHolder holder;
		
		if(v == null){
			
			holder = new ViewHolder();
			LayoutInflater inflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflator.inflate(R.layout.common_details_page, null);
			holder.name  = (TextView) v.findViewById(R.id.text1);
			holder.phno = (TextView) v.findViewById(R.id.text2);
			holder.image = (ImageView) v.findViewById(R.id.imageview);
			holder.rLayout  = (RelativeLayout) v.findViewById(R.id.rrlayout_1);
			v.setTag(holder);
		}else{
			holder = (ViewHolder) v.getTag();
		}
		
		
		  v.setTag(holder);
	      holder.rLayout.setId(position);
		
		
		
		AccountModel bean = listdata.get(position);
		
			holder.name.setText(bean.getName());
			holder.phno.setText(bean.getPhno());
			
			
			holder.rLayout.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					for (int i = 0; i <listdata.size(); i++) {
	                    if (v.getId() == i) {
	                    	checkArray[i] = true;
	                    } else {
	                    	checkArray[i] = false;
	                    }
	                }
	                notifyDataSetChanged();
	            }
				
			});           
			
			if (checkArray[position]) {
	            holder.image.setImageResource(R.drawable.active_account_bg);
	        } else {
	            holder.image.setImageResource(R.drawable.account_tabs_bg);
	        }
		
		return v;
	}

}
